//
//  LSListenSelectCell.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/27.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSListenSelectCell.h"
#import "TYAttributedLabel.h"
@interface LSListenSelectCell ()

@property (nonatomic,strong) UIButton *optionBtn;
@property (nonatomic,strong) TYAttributedLabel *optionText;

@end

@implementation LSListenSelectCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {

        self.backgroundColor = [UIColor whiteColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton *optionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        optionBtn.userInteractionEnabled = NO;
        self.optionBtn = optionBtn;
        [optionBtn setBackgroundImage:[UIImage imageNamed:@"LS-答案原始圆"] forState:UIControlStateNormal];
        [self addSubview:optionBtn];
        [optionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(25);
            make.top.equalTo(self).offset(2);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        
        TYAttributedLabel *optionText = [[TYAttributedLabel alloc] init];
        optionText.numberOfLines = 0;
        self.optionText = optionText;
        optionText.font = [UIFont systemFontOfSize:16.0f];
        [self addSubview:optionText];
        optionText.frame = CGRectMake(70,7,kWidth-100,20);
//        [optionText mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(optionBtn.mas_right).offset(10);
//            make.right.equalTo(self).offset(-25);
//            make.top.equalTo(optionBtn.mas_top).offset(5);
//            make.height.mas_equalTo(16);
//        }];
    }
    
    return self;
}

//- (void)setTextHeight:(CGFloat)textHeight {
//    _textHeight = textHeight;
//    [_optionText mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(_textHeight);
//    }];
//}

- (void)setModel:(LSOption *)model {
    _model = model;
    [_optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_optionBtn setTitle:_model.OptionIndex forState:UIControlStateNormal];
    if (![_model.OptionContent isEqualToString:@""]) {
        _optionText.attributedText = [_model.OptionContent dr_HtmlStringWithFontSize:16.0f];
        [_optionText sizeToFit];
        
    } else {
        NSInteger index = [_model.OptionIndex  characterAtIndex:0];
        UIView *view = [_optionText viewWithTag:index + 1000];
        [view removeFromSuperview];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 70)];
        imageView.tag = index + 1000;
        imageView.image = [UIImage imageWithContentsOfFile:_model.OptionPicture];
        [_optionText addSubview:imageView];
        _optionText.frame = CGRectMake(70,7,kWidth-100,100);
    }
//    [_optionText sizeToFit];
    _model.cellHeight = CGRectGetMaxY(_optionText.frame)+10;
}

- (void)setOptionStyle:(SelectOptionStyle)optionStyle {
    _optionStyle = optionStyle;
    if(_optionStyle==SelectOptionWhite) {
        [self.optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.optionBtn setBackgroundImage:[UIImage imageNamed:@"LS-答案原始圆"] forState:UIControlStateNormal];
        
    } else if(_optionStyle==SelectOptionBlue) {
        [self.optionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.optionBtn setBackgroundImage:[UIImage imageNamed:@"LS-选中答案背景"] forState:UIControlStateNormal];
        
    } else if(_optionStyle==SelectOptionGreen) {
        [self.optionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.optionBtn setBackgroundImage:[UIImage imageNamed:@"LS-选中正确答案背景"] forState:UIControlStateNormal];
        
    } else if(_optionStyle==SelectOptionRed) {
        [self.optionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.optionBtn setBackgroundImage:[UIImage imageNamed:@"LS-选中答案错误背景"] forState:UIControlStateNormal];
    }
}

@end
